package org.lightcommons.orm.hibernate;

public class KeywordCondition implements Condition{
	private String[] keywords;
	private String namePerfix;
	private String[] fields;
	
	public KeywordCondition(String[] keywords, String namePerfix, String[] fields) {
		super();
		this.keywords = keywords;
		this.namePerfix = namePerfix;
		this.fields = fields;
	}

	public boolean isEmpty() {
		return keywords.length==0;
	}
	
	@Override
	public String toString() {
		ConditionGroup andGroup = new ConditionGroup(false);
		for(int i=0;i<keywords.length;i++){
			if(keywords[i].length()==0)continue;
			ConditionGroup orGroup = andGroup.orGroup();
			for(String f : fields){
				orGroup.add(f+" like :"+namePerfix+i);
			}
		}
		return andGroup.toString();
	}
}
